<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      background-color: red;
    }
    #news .router-link-exact-active{
      background-color: rgb(163, 211, 197);
    }
  </style>
</head>
<body>
  <div id="app">
    <!-- 视图上定义 router-view组件 作为路由组件出口 
      router-view定义在哪里，路由组件就在哪里渲染 -->
      <router-link tag="button" to="/aa">首页</router-link>
      <router-link tag="button" to="/news">新闻页</router-link>
      <router-link replace tag="button" to="/about">关于我们页</router-link>
      <hr>
      <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 1 定义路由组件的config对象
    const Home = {
      template: `
        <div>
          <h1>我是home</h1>  
        </div>
      `,
      mounted(){
        console.log(this.$route);
      }
    }
    const News = {
      template: `
        <div id="news">
          <h1>我是新闻</h1>  
          <router-link to="/news/native">国内新闻</router-link>
          <router-link to="/news/abroad">国外新闻</router-link>
          <router-view></router-view>
        </div>
      `
    }
    // 新闻页 子路由组件
    const NativeNews = {
      template: `
        <div>
          <h2>我是国内新闻</h2>  
        </div>
      `
    }
    const AbroadNews = {
      template: `
        <div>
          <h2>我是国外新闻</h2>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h1>我是关于我们</h1>  
        </div>
      `
    }

    const routes = [
      // 单个路由规则 是对象
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        alias: '/aa',
        component: Home
      },
      {
        path: '/news',
        component: News,
        // 定义children属性 定子路由规则
        children: [
          {
            path: '/news',
            redirect: '/news/native'
          },
          {
            path: '/news/native', // 建议携带一级路由 path作为 子路由地址 前缀
            component: NativeNews
          },
          {
            path: '/news/abroad',
            component: AbroadNews
          }
        ]
      },
      {
        path: '/about',
        component: About
      }
    ]
    // 创建路由 实例
    const router = new VueRouter({
      // routes 路由规则
      routes
    })
    // 将 路由实例挂载到vm上
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>
</html>